package com.tjsinfo.tjsoa.mail.bo;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsFoldMailDao;
import com.tjsinfo.tjsoa.mail.dao.TjsemailReceivedDao;
import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmail;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldMail;
import com.tjsinfo.tjsoa.mail.vo.TjsemailReceived;
import com.tjsinfo.tjsoa.mail.vo.Tjsmail;

import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class TjsemailReceivedBo implements ITjsemailReceivedBo{
	private TjsemailReceivedDao tjsemailReceivedDao;
	private ITjsFoldMailDao tjsFoldMailDao;
	private ITjsDeleteEmailBo tjsDeleteEmailBo;
	private IQbcDao qbcDao;
	private IJDBCDao jdbcDao;
	private final boolean STARTTRUE = true;
	private final boolean STARTFALSE = false;
	public IJDBCDao getJdbcDao() {
		return jdbcDao;
	}
	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}
	public ITjsFoldMailDao getTjsFoldMailDao() {
		return tjsFoldMailDao;
	}
	public void setTjsFoldMailDao(ITjsFoldMailDao tjsFoldMailDao) {
		this.tjsFoldMailDao = tjsFoldMailDao;
	}
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public TjsemailReceivedDao getTjsemailReceivedDao() {
		return tjsemailReceivedDao;
	}
	public void setTjsemailReceivedDao(TjsemailReceivedDao tjsemailReceivedDao) {
		this.tjsemailReceivedDao = tjsemailReceivedDao;
	}
	public ITjsDeleteEmailBo getTjsDeleteEmailBo() {
		return tjsDeleteEmailBo;
	}
	public void setTjsDeleteEmailBo(ITjsDeleteEmailBo tjsDeleteEmailBo) {
		this.tjsDeleteEmailBo = tjsDeleteEmailBo;
	}
	/**
	 * 添加收到的邮件

	 * @param 邮件ID
	 * @param 接收人ID
	 * @param 性质
	 * @param 类型
	 * @return
	 */
	public boolean addTjsmailReceived(String mailid,String userid,Integer nature,Integer type,String username)
	{
		try {
			TjsemailReceived tjsemailReceived = new TjsemailReceived();//收的邮件
			tjsemailReceived.setRecNature(nature);
			tjsemailReceived.setRecStart(1);
			tjsemailReceived.setRecType(type);
			tjsemailReceived.setRecTag(0);
			Tjsmail tjsmail = new Tjsmail();
			tjsmail.setTjsmailId(mailid);
			tjsemailReceived.setTjsmail(tjsmail);
			if (userid!=null) {
				tjsemailReceived.setRecUserid(userid);
			}
			tjsemailReceived.setRecUsername(username);
			
			//List<TjsemailReceived> list = tjsemailReceivedDao.findAll();
			//tjsemailReceived.setSortid(list.size()+1);
			
			tjsemailReceivedDao.save(tjsemailReceived);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 修改收信的状态

	 * @param 收到的信的ID
	 * @param 状态

	 * @return
	 */
	public boolean updateTjsmailReceivedStart(String recid,Integer start)
	{
		try {
			TjsemailReceived received = seleceById(recid);//收的邮件
			received.setRecStart(start);
			tjsemailReceivedDao.update(received);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	
	/**
	 * 修改收信的类型

	 * @param 收到的信的ID
	 * @param 类型
	 * @return
	 */
	public boolean updateTjsmailReceivedType(String recid,Integer type)
	{
		try {
			TjsemailReceived received = seleceById(recid);//收的邮件
			received.setRecType(type);
			tjsemailReceivedDao.update(received);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	
	/**
	 * 修改收信的类型

	 * @param 收到的信的ID
	 * @param 类型
	 * @return
	 */
	public boolean updateTjsmailReceivedType(String[] recidarray,Integer type)
	{
		try {
			for (int i = 0; i < recidarray.length; i++) {
				TjsemailReceived received = seleceById(recidarray[i]);//收的邮件
				received.setRecType(type);
				tjsemailReceivedDao.update(received);
			}
			
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	
	/**
	 * 依据主键查询
	 * @param id
	 * @return
	 */
	public List<TjsemailReceived> selectByUserId(String recUserid){
		return tjsemailReceivedDao.selectReceivedMailById(recUserid);
	}
	public TjsemailReceived seleceById(String recid)
	{
		//DetachedCriteria detachedCriteria  = DetachedCriteria.forClass(TjsemailReceived.class);
		//detachedCriteria.add(Expression.eq("recId", recid));
		List<TjsemailReceived> list 	= tjsemailReceivedDao.selectReceivedMailById(recid);		
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	/*public Integer count(Integer recstart){
		String sql="select count(*) from TjsemailReceived rec where rec.recStart =1";
		return jdbcDao.executeUpdate(sql);
	}*/
	public Integer count(String userid,Integer recstart){
		//Object[] recstart1 = {recstart};
		return tjsemailReceivedDao.countReceivedMail(userid,recstart).size();
	}
	/**
	 * 删除收到的邮件

	 * @return
	 */
	public boolean deleteTjsReceived(String recid)
	{
		try {
			TjsemailReceived received = selectTjsReceivedById(recid);//收的邮件
			/*TjsDeleteEmail deleteEmail = new TjsDeleteEmail();
			deleteEmail.setTismailRead(received.getRecStart());
			deleteEmail.setTismailTag(received.getRecTag());
			deleteEmail.setTjsmailDatatime(new Date());
			deleteEmail.setTjsmailStart(0);
			deleteEmail.setTjsmailTitle(received.getTjsmail().getTjsmailTitle());
			deleteEmail.setTjsmailType(received.getRecType());
			deleteEmail.setTjsmailValue(received.getTjsmail().getTjsmailValue());
			deleteEmail.setTjsmailUserid(Integer.valueOf(received.getRecUserid()).intValue());
			deleteEmail.setTjsmailUsername(received.getRecUsername());*/
		/*	List list = received.getZhusonglist();
			String zhusong = "";
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				TjsemailReceived received2 = (TjsemailReceived) iterator.next();
				
				zhusong+=",";
				zhusong+=received2.getTjsUser().getUserName();
			}
			String chaosong = "";
			List list2 = received.getChaosonglist();
			for (Iterator iterator = list2.iterator(); iterator.hasNext();) {
				TjsemailReceived received2 = (TjsemailReceived) iterator.next();
				
				chaosong+=",";
				chaosong+=received2.getTjsUser().getUserName();
			}
			deleteEmail.setTjsmailzhusong(zhusong);
			deleteEmail.setTjsmailchaosong(chaosong);*/
			/*Set set2 = received.getTjsmail().getTjsAnnexes();//收的邮件的附件

			for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
				TjsAnnex annex = (TjsAnnex) iterator.next();
				TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
				emailAnnex.setAnnexAddress(annex.getAnnexAddress());
				emailAnnex.setAnnexName(annex.getAnnexName());
				emailAnnex.setAnnexSize(annex.getAnnexSize());
				emailAnnex.setZhenname(annex.getAnnexZhenName());
				deleteEmail.addAnnexes(emailAnnex);
				
			}
			tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);*/
			tjsemailReceivedDao.delete(received);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 删除收到的邮件

	 * @param 收信的ID
	 * @return
	 */
/*	public boolean deleteTjsReceived(String[] recid)
	{
		try {
			for (int i = 0; i < recid.length; i++) {
				TjsemailReceived received = selectTjsReceivedById(recid[i]);
				TjsDeleteEmail deleteEmail = new TjsDeleteEmail();
				deleteEmail.setTismailRead(received.getRecStart());
				deleteEmail.setTismailTag(received.getRecTag());
				deleteEmail.setTjsmailDatatime(new Date());
				deleteEmail.setTjsmailStart(0);
				deleteEmail.setTjsmailTitle(received.getTjsmail().getTjsmailTitle());
				deleteEmail.setTjsmailType(received.getRecType());
				deleteEmail.setTjsmailValue(received.getTjsmail().getTjsmailValue());
				deleteEmail.setTjsUser(received.getTjsUser());
				String zhusong = "";
				List list = received.getZhusonglist();
				for (Iterator iterator = list.iterator(); iterator.hasNext();) {
					TjsemailReceived received2 = (TjsemailReceived) iterator.next();
					
					zhusong+=",";
					zhusong+=received2.getTjsUser().getUserName();
				}
				String chaosong = "";
				List list2 = received.getChaosonglist();
				for (Iterator iterator = list2.iterator(); iterator.hasNext();) {
					TjsemailReceived received2 = (TjsemailReceived) iterator.next();
					
					chaosong+=",";
					chaosong+=received2.getTjsUser().getUserName();
				}
				deleteEmail.setTjsmailzhusong(zhusong);
				deleteEmail.setTjsmailchaosong(chaosong);
				Set set2 = received.getTjsmail().getTjsAnnexes();
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsAnnex annex = (TjsAnnex) iterator.next();
					TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setZhenname(annex.getAnnexZhenName());
					deleteEmail.addAnnexes(emailAnnex);
				}
				tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);
				tjsemailReceivedDao.delete(received);
			}
				return STARTTRUE;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
		}*/
	/**
	 * 删除收到的邮件

	 * @param 收信的ID
	 * @return
	 */
	public boolean deleteTjsReceived(String[] recidarray)
	{
		try {
			for (int i = 0; i < recidarray.length; i++) {
				TjsemailReceived received = tjsemailReceivedDao.selectReceivedListById(recidarray[i]).get(0);//收的邮件
				TjsDeleteEmail deleteEmail = new TjsDeleteEmail();//删除的邮件

				deleteEmail.setTismailRead(received.getRecStart());
				deleteEmail.setTismailTag(received.getRecTag());
				/*if(received.getRecTag() == null)
				{
					deleteEmail.setTismailTag(0);
				}
				else
				{
					deleteEmail.setTismailTag(received.getRecTag());
				}*/
				deleteEmail.setTjsmailDatatime(new Date());
				deleteEmail.setTjsmailStart(0);
				deleteEmail.setTjsmailTitle(received.getTjsmail().getTjsmailTitle());
				deleteEmail.setTjsmailType(received.getRecType());
				deleteEmail.setTjsmailValue(received.getTjsmail().getTjsmailValue());
				deleteEmail.setTjsmailUserid(received.getTjsmail().getTjsmailUserid());
				//Integer.valueOf(received.getRecUserid()).intValue()
				deleteEmail.setTjsmailUsername(received.getRecUsername());
				deleteEmail.setTjsmailsendren(received.getTjsmail().getTjsmailUsername());
				String zhusong = "";
			/*	List list = received.getZhusonglist();
				for (Iterator iterator = list.iterator(); iterator.hasNext();) {
					TjsemailReceived received2 = (TjsemailReceived) iterator.next();
					
					zhusong+=",";
					zhusong+=received2.getTjsUser().getUserName();
				}
				String chaosong = "";
				List list2 = received.getChaosonglist();
				for (Iterator iterator = list2.iterator(); iterator.hasNext();) {
					TjsemailReceived received2 = (TjsemailReceived) iterator.next();
					
					chaosong+=",";
					chaosong+=received2.getTjsUser().getUserName();
				}
				deleteEmail.setTjsmailzhusong(zhusong);
				deleteEmail.setTjsmailchaosong(chaosong);*/
				Set set2 = received.getTjsmail().getTjsAnnexes();//收的邮件的附件

				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsAnnex annex = (TjsAnnex) iterator.next();
					TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setZhenname(annex.getAnnexZhenName());
					deleteEmail.addAnnexes(emailAnnex);
				}
				tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);
				received.setIsdel(1);
				tjsemailReceivedDao.delete(received);
			}
				return STARTTRUE;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
		}		
	
	/**
	 * 查询收件人的收件列表
	 * @param 用户ID
	 * @return 
	 */
	public List<TjsemailReceived> selectUserId(String id,String username) {
		// TODO Auto-generated method stub
		return tjsemailReceivedDao.selectReceivedListByUserId(id,username);
	}
	/**
	 * 批量修改收信的状态

	 * @param 收到的信的ID
	 * @param 状态

	 * @return
	 */
	public boolean updateTjsmailReceivedStartList(String[] recidarray,
			Integer start) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < recidarray.length; i++) {
				if(recidarray[i] != null)
				{
					TjsemailReceived received = seleceById(recidarray[i]);//收的邮件
					received.setRecStart(start);
					tjsemailReceivedDao.update(received);
				}
				
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 批量修改收信的标签

	 * @param 收到的信的ID
	 * @param 状态

	 * @return
	 */
	public boolean updateTjsmailReceivedTagList(String[] recidarray,
			Integer start) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < recidarray.length; i++) {
				TjsemailReceived received = seleceById(recidarray[i]);
				received.setRecTag(start);
				tjsemailReceivedDao.update(received);
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 根据ID查询收件同时抓取
	 * @param id
	 * @return
	 */
	private TjsemailReceived selectTjsReceivedById(String id)
	{
		return  tjsemailReceivedDao.selectReceivedListById(id).get(0);
	}
	/**
	 * 移动到指定文件夹
	 */
	public boolean moveTjsReceived(String[] recid,String foldid) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < recid.length; i++) {
				TjsemailReceived received = selectTjsReceivedById(recid[i] );//收的邮件
				TjsFoldMail tjsFoldMail = new TjsFoldMail();//文件夹的邮件
				tjsFoldMail.setTjsmailRead(received.getRecStart());
				if (received.getRecTag() != null) {
					tjsFoldMail.setTjsmailTag(received.getRecTag());
				}
				else 
				{
					tjsFoldMail.setTjsmailTag(0);
				}
				tjsFoldMail.setTjsmailDatetime(new Date());
				tjsFoldMail.setTjsmailStart(0);
				tjsFoldMail.setTjsmailTitle(received.getTjsmail().getTjsmailTitle());
				tjsFoldMail.setTjsmailType(received.getRecType());
				tjsFoldMail.setTjsmailValue(received.getTjsmail().getTjsmailValue());
				tjsFoldMail.setTjsmailUsername(received.getTjsmail().getTjsmailUsername());
				TjsEmailFolder tjsEmailFolder = new TjsEmailFolder();
				tjsEmailFolder.setFolderId(foldid);
				tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
				tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
				//tjsFoldMail.setTjsUser(received.getTjsUser());
				Set set2 = received.getTjsmail().getTjsAnnexes();//收的邮件的附件

				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsAnnex annex = (TjsAnnex) iterator.next();
					TjsFoldEmailAnnex   emailAnnex = new TjsFoldEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setAnnexZhenName(annex.getAnnexZhenName());
					tjsFoldMail.addTjsFoldMailAnnex(emailAnnex);
				}
				tjsFoldMailDao.save(tjsFoldMail);
				tjsemailReceivedDao.delete(received);
			}
				return STARTTRUE;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
	}
	/**
	 * 查询收件的具体信息

	 * @param 邮件ID
	 * @param 用户ID
	 * @return
	 */
	public TjsemailReceived selectReceivedMailById(String emailid,
			String userid,String username) {
		// TODO Auto-generated method stub
		Object[] parm = {emailid,userid,username};
		List<TjsemailReceived> list = tjsemailReceivedDao.selectReceivedMailById(parm);
		if (list.size() > 0) {
			TjsemailReceived tjsemailReceived  = list.get(0);
			Object[] parm2 = {emailid,1};
			tjsemailReceived.setZhusonglist(tjsemailReceivedDao.selectReceivedMailStart(parm2));
			Object[] parm3 = {emailid,2};
			tjsemailReceived.setChaosonglist(tjsemailReceivedDao.selectReceivedMailStart(parm3));
			return tjsemailReceived;
		} else {
			TjsemailReceived tjsemailReceived = new TjsemailReceived();
			return tjsemailReceived;
		}
	}
	/**
	 * 根据标题查询收件
	 * @param 关键字

	 * @return
	 */
	public List<TjsemailReceived> selectReceivedMailTitile(String title,String userid)
	{
		Object[] object = {"%"+title+"%",userid};
		return  tjsemailReceivedDao.selectReceivedMailTitile(object);
	}
	
	/**
	 * 查询未读邮件
	 * @param userid
	 * @return
	 */
	public List<TjsemailReceived> selectNewUserId(String userid,Integer recstart){
		return tjsemailReceivedDao.countReceivedMail(userid,recstart);
	}
	
	public Integer selectWeiReadEmail(String username)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(TjsemailReceived.class);
		criteria.add(Restrictions.eq("recStart", 1));
		criteria.add(Restrictions.eq("recUsername", username));
		
		return qbcDao.getCriteria(criteria).size();
	}
	public List<TjsemailReceived> seleceByemailId(String emailId) {
		// TODO Auto-generated method stub
		List<TjsemailReceived> list 	= tjsemailReceivedDao.selectReceivedMailByemailId(emailId);		
		if (list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}
	
}
